package org.geektimes.projects.user.validator.bean.validation;

import org.apache.commons.lang.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author tom
 * @since 2021/3/10 19:12
 */
public class StrLenValidator implements ConstraintValidator<StrLen, String> {

    private int min;

    private int max;

    @Override
    public void initialize(StrLen strLen) {
        this.min = strLen.min();
        this.max = strLen.max();
    }

    @Override
    public boolean isValid(String source, ConstraintValidatorContext constraintValidatorContext) {
        if(StringUtils.isBlank(source)){
            return false;
        }
        int len = source.length();
        return len >= min && len <= max;
    }
}
